package com.jxsd.edu.cn.struts2.daoImpl;

import com.jxsd.edu.cn.struts2.dao.UserDao;
import com.jxsd.edu.cn.struts2.models.Loginview;
import com.jxsd.edu.cn.struts2.models.UserInfo;
import com.jxsd.edu.cn.tools.Page;
import com.jxsd.edu.cn.tools.PageBean;
import com.jxsd.edu.cn.tools.ResultBean;
import com.jxsd.edu.cn.tools.ResultList;
import com.jxsd.edu.cn.tools.StringTool;
import com.jxsd.edu.cn.tools.Template;


public class UserDaoImpl implements UserDao 
{
	@Override
	public Loginview getLoginUser(String attributeName,String  attriValue) 
	{
		ResultBean<Loginview>  bean=new ResultBean<Loginview>();
		String  sql="select userId,nickName,userpassword  from  loginview where "+attributeName+"=?";
		Object[]  params={attriValue};
		Loginview  login=null;
		try {
			login=bean.handler(sql, params, Loginview.class);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return login;
	}

	@Override
	public UserInfo getUserInfo(String paramName, String value) {
		ResultBean<UserInfo>  bean=new ResultBean<UserInfo>();
		String  sql="select userId,userPassword,nickName," +
				"telephone,Image,QQ,lastvisist,regisTime,email,cityName,univerName  from  userInfo  where "+paramName+"=?";
		Object[]  params={value};
		UserInfo  user=null;
		try {
			user=bean.handler(sql, params, UserInfo.class);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return user;
	}

	@Override
	public Page<UserInfo> getUsers(PageBean<UserInfo> pagebean) {
		ResultList<UserInfo>  list=new  ResultList<UserInfo>();
	    String  sql="select userId,userPassword,nickName," +
				"telephone,Image,QQ,lastvisist,regisTime,email,cityName,univerName  from  userInfo ";
	    Object[]  params=null;
	    Page<UserInfo>  page=null;
	    try {
			page=list.pageHandler(pagebean, sql, params, UserInfo.class);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return page;
	}

	@Override
	public void deleteUser(String userId) {
		String  sql="delete  from  userInfo  where  userId=?";
		Object[]  params={userId};
		Template  template=new Template(); 
		template.update(sql, params);
	}

	@Override
	public void updateUser(UserInfo user) {
		String  sql="update  userInfo  set  nickName=?,  telephone=?," +
				"  Image=?, QQ=?,  cityName=?,univerName=?,email=?  where  userId=?";
		
		System.out.println("UserDaoImpl:updateUser()**********SQL:");
		System.out.println(sql);
		
		
		System.out.println(user.getNickName());
		System.out.println(user.getTelephone());
		System.out.println(user.getImage());
		System.out.println(user.getQQ());
		System.out.println(user.getUniverName());
		System.out.println(user.getUserId());
		
		
		System.out.println("UserDaoImpl:updateUser()**********SQL:");
		
		
		Object[]  params={user.getNickName(),user.getTelephone()
				          ,user.getImage(),user.getQQ(),user.getCityName(),user.getUniverName(),user.getEmail(),user.getUserId()};
		Template  template=new Template(); 
		template.update(sql, params);
		
	}

	@Override
	public boolean checkUserByNick(String nickName) 
	{
	   String  sql="select  userId,nickName,userpassword  from  loginview  where  nickName=?";
	   ResultBean<Loginview>  bean=new ResultBean<Loginview>();
	   Loginview login=null;
	   Object[] params={nickName};
	   try {
		   login=bean.handler(sql, params, Loginview.class);
	} catch (Exception e) {
		e.printStackTrace();
	}
	   
	   return  null==login? false:true;
	}

	@Override
	public void modifyPassword(UserInfo  user) {
		String  sql="update  userInfo  set  userPassword=?  where  userId=?";
		Object[]  params={user.getUserPassword(),user.getUserId()};
		Template  template=new Template(); 
		template.update(sql, params);
	}

	@Override
	public void addUser(UserInfo user) {
		String  sql="insert  into  UserInfo(UserPassword,nickname,email,regisTime,lastvisist)" +
				"values(?,?,?,?,?)";
		Object[] params={user.getUserPassword(),user.getNickName(),user.getEmail(),StringTool.getCurDate(),StringTool.getCurDate()};
		Template  template=new Template(); 
		template.update(sql, params);
	}

	@Override
	public void updateImage(UserInfo user) {
		String  sql="update  userInfo  set  IMAGE=?  where  userId=?";
		Object[]  params={user.getImage(),user.getUserId()};
		Template  template=new Template(); 
		template.update(sql, params);
	}

}
